package com.antis.olsl.data.stock;

import android.text.TextUtils;

import com.antis.olsl.base.net.BaseSubscriber;
import com.antis.olsl.base.net.ExceptionHandle;
import com.antis.olsl.base.utils.LogUtils;
import com.antis.olsl.base.utils.RxUtil;
import com.antis.olsl.data.net.ApiServer;
import com.antis.olsl.data.net.RetrofitManager;
import com.antis.olsl.data.response.GetBrandInventoryListResp;
import com.antis.olsl.data.response.GetCategoryInventoryListResp;
import com.antis.olsl.data.response.GetProductInventoryListResp;
import com.antis.olsl.library.MyApplication;
import com.antis.olsl.library.response.GetSalesroomOrWarehouseListRes;
import com.antis.olsl.library.utils.RSAEncryptUtil;

import java.util.Map;

public class StockQueryPresenter implements StockQueryContract.Presenter {
    private StockQueryContract.View mView;
    private ApiServer mApiServer;

    public StockQueryPresenter() {
        mApiServer = RetrofitManager.getInstance().getServerApi();
    }

    @Override
    public void takeView(StockQueryContract.View view) {
        mView = view;
    }

    @Override
    public void getSalesroomInfoList(Map<String, Object> map) {
        String encryptString = RSAEncryptUtil.generateEncryptStr(map);
        LogUtils.e("hhh", "getSalesroomInfoList map= " + map);
        String token = null != MyApplication.getUserInfo() && !TextUtils.isEmpty(MyApplication.getUserInfo().getToken()) ? MyApplication.getUserInfo().getToken() : "123456";

        mApiServer.getSalesroomInfoList(encryptString, token, 2).compose(RxUtil.<GetSalesroomOrWarehouseListRes>applySchedulers(RxUtil.IO_ON_UI_TRANSFORMER_BACK_PRESSURE))
                .compose(mView.bindToLife())
                .subscribe(new BaseSubscriber<GetSalesroomOrWarehouseListRes>() {
                    @Override
                    public void onNext(GetSalesroomOrWarehouseListRes resp) {
                        if (resp.isOk()) {
                            if (null != resp.getContent() && resp.getContent().size() > 0) {
                                mView.getSalesroomOrWarehouseInfoListSuccess(resp.getContent());
                            }
                        } else if (resp.isTimeOut()) {
                            mView.getSalesroomOrWarehouseInfoListFailure("请求超时");
                        } else if (resp.isSlowNetwork()) {
                            mView.getSalesroomOrWarehouseInfoListFailure("网络缓慢，稍后重试");
                        } else if (resp.isUnauthorized()) {
                            mView.getSalesroomOrWarehouseInfoListFailure("接口未授权");
                        } else {
                            mView.getSalesroomOrWarehouseInfoListFailure("请求失败");
                        }
                    }

                    @Override
                    protected void hideDialog() {

                    }

                    @Override
                    protected void showDialog() {

                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseThrowable e) {
                        if (null != e) {
                            if (!TextUtils.isEmpty(e.message)) {
                                mView.getSalesroomOrWarehouseInfoListFailure(e.message);
                            } else {
                                mView.getSalesroomOrWarehouseInfoListFailure("请求失败");
                            }
                        } else {
                            mView.getSalesroomOrWarehouseInfoListFailure("请求失败");
                        }
                    }
                });
    }

    @Override
    public void getWarehouseInfoList(Map<String, Object> map) {
        String encryptString = RSAEncryptUtil.generateEncryptStr(map);
        LogUtils.e("hhh", "getWarehouseInfoList map= " + map);
        String token = null != MyApplication.getUserInfo() && !TextUtils.isEmpty(MyApplication.getUserInfo().getToken()) ? MyApplication.getUserInfo().getToken() : "123456";

        mApiServer.getWarehouseInfoList(encryptString, token, 2).compose(RxUtil.<GetSalesroomOrWarehouseListRes>applySchedulers(RxUtil.IO_ON_UI_TRANSFORMER_BACK_PRESSURE))
                .compose(mView.bindToLife())
                .subscribe(new BaseSubscriber<GetSalesroomOrWarehouseListRes>() {
                    @Override
                    public void onNext(GetSalesroomOrWarehouseListRes resp) {
                        if (resp.isOk()) {
                            if (null != resp.getContent() && resp.getContent().size() > 0) {
                                mView.getSalesroomOrWarehouseInfoListSuccess(resp.getContent());
                            }
                        } else if (resp.isTimeOut()) {
                            mView.getSalesroomOrWarehouseInfoListFailure("请求超时");
                        } else if (resp.isSlowNetwork()) {
                            mView.getSalesroomOrWarehouseInfoListFailure("网络缓慢，稍后重试");
                        } else if (resp.isUnauthorized()) {
                            mView.getSalesroomOrWarehouseInfoListFailure("接口未授权");
                        } else {
                            mView.getSalesroomOrWarehouseInfoListFailure("请求失败");
                        }
                    }

                    @Override
                    protected void hideDialog() {

                    }

                    @Override
                    protected void showDialog() {

                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseThrowable e) {
                        if (null != e) {
                            if (!TextUtils.isEmpty(e.message)) {
                                mView.getSalesroomOrWarehouseInfoListFailure(e.message);
                            } else {
                                mView.getSalesroomOrWarehouseInfoListFailure("请求失败");
                            }
                        } else {
                            mView.getSalesroomOrWarehouseInfoListFailure("请求失败");
                        }
                    }
                });
    }

    @Override
    public void getProductInventoryList(Map<String, Object> map) {
        String encryptString = RSAEncryptUtil.generateEncryptStr(map);
        LogUtils.e("getProductInventoryList map= " + map);
        String token = null != MyApplication.getUserInfo() && !TextUtils.isEmpty(MyApplication.getUserInfo().getToken()) ? MyApplication.getUserInfo().getToken() : "123456";

        mApiServer.getProductInventoryList(encryptString, token, 2).compose(RxUtil.<GetProductInventoryListResp>applySchedulers(RxUtil.IO_ON_UI_TRANSFORMER_BACK_PRESSURE))
                .compose(mView.bindToLife())
                .subscribe(new BaseSubscriber<GetProductInventoryListResp>() {
                    @Override
                    public void onNext(GetProductInventoryListResp resp) {
                        if (resp.isOk()) {
                            if (null != resp.content) {
                                mView.getProductInventoryListSuccess(resp);
                            }
                        } else if (resp.isTimeOut()) {
                            mView.getProductInventoryListFailure("请求超时");
                        } else if (resp.isSlowNetwork()) {
                            mView.getProductInventoryListFailure("网络缓慢，稍后重试");
                        } else if (resp.isUnauthorized()) {
                            mView.getProductInventoryListFailure("接口未授权");
                        } else {
                            mView.getProductInventoryListFailure("请求失败");
                        }
                    }

                    @Override
                    protected void hideDialog() {

                    }

                    @Override
                    protected void showDialog() {

                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseThrowable e) {
                        if (null != e) {
                            if (!TextUtils.isEmpty(e.message)) {
                                mView.getProductInventoryListFailure(e.message);
                            } else {
                                mView.getProductInventoryListFailure("请求失败");
                            }
                        } else {
                            mView.getProductInventoryListFailure("请求失败");
                        }
                    }
                });
    }

    @Override
    public void getBrandInventoryList(Map<String, Object> map) {
        String encryptString = RSAEncryptUtil.generateEncryptStr(map);
        LogUtils.e("getBrandInventoryList map= " + map);
        String token = null != MyApplication.getUserInfo() && !TextUtils.isEmpty(MyApplication.getUserInfo().getToken()) ? MyApplication.getUserInfo().getToken() : "123456";

        mApiServer.getBrandInventoryList(encryptString, token, 2).compose(RxUtil.<GetBrandInventoryListResp>applySchedulers(RxUtil.IO_ON_UI_TRANSFORMER_BACK_PRESSURE))
                .compose(mView.bindToLife())
                .subscribe(new BaseSubscriber<GetBrandInventoryListResp>() {
                    @Override
                    public void onNext(GetBrandInventoryListResp resp) {
                        if (resp.isOk()) {
                            if (null != resp.content) {
                                mView.getBrandInventoryListSuccess(resp);
                            }
                        } else if (resp.isTimeOut()) {
                            mView.getBrandInventoryListFailure("请求超时");
                        } else if (resp.isSlowNetwork()) {
                            mView.getBrandInventoryListFailure("网络缓慢，稍后重试");
                        } else if (resp.isUnauthorized()) {
                            mView.getBrandInventoryListFailure("接口未授权");
                        } else {
                            mView.getBrandInventoryListFailure("请求失败");
                        }
                    }

                    @Override
                    protected void hideDialog() {

                    }

                    @Override
                    protected void showDialog() {

                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseThrowable e) {
                        if (null != e) {
                            if (!TextUtils.isEmpty(e.message)) {
                                mView.getBrandInventoryListFailure(e.message);
                            } else {
                                mView.getBrandInventoryListFailure("请求失败");
                            }
                        } else {
                            mView.getBrandInventoryListFailure("请求失败");
                        }
                    }
                });
    }

    @Override
    public void getCategoryInventoryList(Map<String, Object> map) {
        String encryptString = RSAEncryptUtil.generateEncryptStr(map);
        LogUtils.e("getCategoryInventoryList map= " + map);
        String token = null != MyApplication.getUserInfo() && !TextUtils.isEmpty(MyApplication.getUserInfo().getToken()) ? MyApplication.getUserInfo().getToken() : "123456";

        mApiServer.getCategoryInventoryList(encryptString, token, 2).compose(RxUtil.<GetCategoryInventoryListResp>applySchedulers(RxUtil.IO_ON_UI_TRANSFORMER_BACK_PRESSURE))
                .compose(mView.bindToLife())
                .subscribe(new BaseSubscriber<GetCategoryInventoryListResp>() {
                    @Override
                    public void onNext(GetCategoryInventoryListResp resp) {
                        if (resp.isOk()) {
                            if (null != resp.content) {
                                mView.getCategoryInventoryListSuccess(resp);
                            }
                        } else if (resp.isTimeOut()) {
                            mView.getCategoryInventoryListFailure("请求超时");
                        } else if (resp.isSlowNetwork()) {
                            mView.getCategoryInventoryListFailure("网络缓慢，稍后重试");
                        } else if (resp.isUnauthorized()) {
                            mView.getCategoryInventoryListFailure("接口未授权");
                        } else {
                            mView.getCategoryInventoryListFailure("请求失败");
                        }
                    }

                    @Override
                    protected void hideDialog() {

                    }

                    @Override
                    protected void showDialog() {

                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseThrowable e) {
                        if (null != e) {
                            if (!TextUtils.isEmpty(e.message)) {
                                mView.getCategoryInventoryListFailure(e.message);
                            } else {
                                mView.getCategoryInventoryListFailure("请求失败");
                            }
                        } else {
                            mView.getCategoryInventoryListFailure("请求失败");
                        }
                    }
                });
    }

    @Override
    public void subscribe() {

    }

    @Override
    public void unSubscribe() {
        LogUtils.e("StockQueryPresenter", "unSubscribe");
        mView = null;
    }
}
